// pages/enquiry/index.js
import request from '../../../baseRequest'
import urls from '../../../requestUrl'
import { $Message } from '../../../components/base/index'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        uploadInfo: {
            title: '行驶证图片上传',
            count: 1,
            attentionText: '1.行驶证图片仅需上传一张',
            uploadUrl: urls.baseUrl + '/api/insurance/image/upload',
            isUpload: false
        },
    },
    /**监听完成图片上传 */
    uploadCompleteListence({ detail }) {
        console.log('detail', detail)
        this.handleRequestEnquiry(detail)
    },
    /**处理点击“提交询价”事件 */
    handleToEnquiry(e) {
        console.log(e)
        let imgs = e.detail.value.imgs
        if (imgs.length === 0) {
            this.showErrorMsg('请按要求上传行驶证图片')
        } else if (imgs.length > 1) {
            this.showErrorMsg('行驶证仅需上传一张图片')
        } else {
            /**开始上传 */
            this.setData({
                ['uploadInfo.isUpload']: true
            })
        }

    },

    /**请求询价接口 */
    handleRequestEnquiry(detail) {
        //请求参数
        let param = {
            driverLicenseImg: detail,
            openid: this.data.openid
        }
        console.log(param)

        request.postRequest(urls.enquiry.request, param, (res) => {
            wx.showModal({
                title: '询价提交成功',
                content: '我们的专业人员会尽快为您提供报价,请耐心等候！',
                showCancel: false,
                success: r => {
                    wx.redirectTo({
                        url: '/pages/enquiry/detail/index?id=' + res.id,
                    })
                }
            })
        }, () => {})
    },

    /**
     * 展示错误信息提示
     * @param {*} msg 
     */
    showErrorMsg(msg) {
        $Message({
            content: msg,
            type: 'error'
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.openid = getApp().getUserOpenid()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})